package net.wicp.tams.duckula.server.web;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.common.connector.BusiUtil;
import net.wicp.tams.common.connector.beans.CusDynaBean;
import net.wicp.tams.common.connector.constant.param.Request;
import net.wicp.tams.common.connector.constant.param.Response;
import net.wicp.tams.common.connector.executor.IConfigManager;
import net.wicp.tams.common.connector.executor.impl.CommonService;
import net.wicp.tams.commons.apiext.StringUtil;
import net.wicp.tams.commons.exception.ExceptAll;
import net.wicp.tams.duckula.server.constant.Command;

@Controller
@RestController
@RequestMapping(value = { "/" })
@Configuration
@Slf4j
public class DuckulaController {

	@Autowired
	public IConfigManager configmanager;
	@Autowired
	private CommonService commonService;

	@RequestMapping(value = "/taskstart", method = RequestMethod.POST)
	public String taskstart(@RequestBody String requestBody, HttpServletResponse response) {
		String outbean = doCommond(Command.TaskStart, requestBody, response);
		return outbean;
	}

	@RequestMapping(value = "/taskstop", method = RequestMethod.POST)
	public String taskstop(@RequestBody String requestBody, HttpServletResponse response) {
		String outbean = doCommond(Command.TaskStop, requestBody, response);
		return outbean;
	}

	@RequestMapping(value = "/monitorshow", method = RequestMethod.POST)
	public String monitorshow(@RequestBody String requestBody, HttpServletResponse response) {
		String outbean = doCommond(Command.MonitorShow, requestBody, response);
		return outbean;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private String doCommond(Command command, String requestBody, HttpServletResponse response) {
		CusDynaBean outbean = null;
		try {
			CusDynaBean inputBean = configmanager.getInputBean(command.getKey());
			inputBean.setByJson(JSONObject.parseObject(requestBody));
			CusDynaBean controlInfo = (CusDynaBean) inputBean.get(Request.controlInfo);
			if (controlInfo == null) {// 没有控制信息
				response.setStatus(ExceptAll.conn_nocontrol.getHttpCode());
				outbean = configmanager.getConfig(command.getKey()).newOutBean(ExceptAll.conn_nocontrol);
				return outbean.getJsonObj().toJSONString();
			}
			String channel = controlInfo.getStrValueByName(Request.senderChannel);
			if (StringUtil.isNull(channel)) {// 没有渠道
				response.setStatus(ExceptAll.conn_nochannel.getHttpCode());
				outbean = configmanager.getConfig(command.getKey()).newOutBean(ExceptAll.conn_nochannel);
				return outbean.getJsonObj().toJSONString();
			}

			// 调服务
			outbean = commonService.exe(command.getKey(), inputBean);
			// result
			if (outbean == null) {
				response.setStatus(ExceptAll.project_other.getHttpCode());
				outbean = configmanager.getConfig(command.getKey()).newOutBean(ExceptAll.project_other);
				outbean.set(Response.errMsg, "服务出错了");
			} else {
				Object httpstatusobj = outbean.get(Response.http);
				log.info("-----------------------------http状态:" + httpstatusobj + "-----------------------");
				int status = BusiUtil.getHttp(String.valueOf(httpstatusobj));
				response.setStatus(status);
			}
			return outbean.getJsonObj().toJSONString();
		} catch (Throwable e) {
			log.error("执行服务错误", e);
			outbean = configmanager.getConfig(command.getKey()).newOutBean(ExceptAll.project_serviceerror);
			outbean.set(Response.errMsg, e.getMessage());
			response.setStatus(BusiUtil.getHttp(ExceptAll.project_serviceerror));
		}
		return outbean.getJsonObj().toJSONString();
	}

}
